package com.tzy.for10evalue.designpattern.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author tzy
 * @date 2022/5/26 19:20
 */
@Component
public class ServiceFactory {
    private static final Map<Long, String> beanNamesMap = new ConcurrentHashMap<>();

    static {
        ServiceEnum[] activityEnums = ServiceEnum.values();
        for (ServiceEnum activityEnum : activityEnums) {
            beanNamesMap.put(activityEnum.getActivityId(), activityEnum.getBeanName());
        }
    }

    @Autowired
    private Map<String, Send> executorMap;

    //工厂层执行器
    public void execute(Long activityId, String mobile, String content) {
        String beanNames = beanNamesMap.get(activityId);
        if (StringUtils.isEmpty(beanNames)) {
            return;
        }
        String[] beanNameArray = beanNames.split(",");
        //决定最终走哪个类的执行器
        for (String beanName : beanNameArray) {
            Send send = executorMap.get(beanName);
            if (send == null) {
                return;
            }
            send.sendMessage(mobile, content);
        }
    }

}
